<?php

class Welcome extends Controller
{
	function __construct()
	{
		parent::__construct();
	}
	
	// http://localhost/chilik/welcome/phonebook
	
	function phonebook()
	{
		$user = $this->load('model', 'user');
		
		$user->db_connect();
		
		$firstname = '';
		$firstname = @$_REQUEST['firstname'];
		$lastname = @$_REQUEST['lastname'];
		$phone = @$_REQUEST['phone'];
		
		if(strlen($firstname) > 0)
		{
			$message = $user->insert_entry(array($firstname, $lastname, $phone));
		}
		
		if(@$_REQUEST['action']=="del")
		{
			$message = $user->delete_entry(array($firstname));
		}
		
		$userlist = $user->get_all();
		$user->db_close();
		
		$data = array(
			'page_title' => 'Phone Book',
			'assets_url' => '/assets/',
			'phonebook_head' => 'Phone Book (Example for PHP+SQLite)',
			'phonebook_text1' => 'A very simple phone book script.',
			'phonebook_text2' => 'But implemented with a very modern and up-to-date technology: SQLite, the SQL database without server.',
			
			'phonebook_head1' => 'My phone numbers',
			'phonebook_head2' => 'Add entry',
			'phonebook_attrib1' => 'Last name',
			'phonebook_attrib2' => 'First name',
			'phonebook_attrib3' => 'Phone number',
			'phonebook_attrib4' => 'Command',
			'phonebook_button1' => 'DELETE',
			'phonebook_button2' => 'Add',
			
			'users' => $userlist,
		);
		
		View::Factory()->render('phonebook', $data);
	}
	
	function index()
	{
		$data = array(
			'chilik MVC' => 'Welcome',
			'version' => '1.2'
		);
		
		View::Factory()->response($data, 200);
	}
	
	function smarty()
	{
		View::Factory()
		->render('welcome_message', 
			array(
				'elapsed_time' => $this::elapsed()
				)
			);
	}
}

